﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace NAnt.SVN.Tasks.Constants
{
    /// <summary>
    /// Util class to store static fields and values in. This 
    /// class provides some reflection functions to check the validity 
    /// of the given fieldnames.
    /// $Id$
    /// </summary>
    public class SVNStaticConstants
    {
         private List<string> allstaticfields;

        /// <summary>
        /// Private ctor to avoid initialisation
        /// </summary>
        protected SVNStaticConstants() { }

        /// <summary>
        /// Get a list with all the commands
        /// </summary>
        public List<string> AllStaticFields
        {
            get {
                if (allstaticfields == null)
                {
                    allstaticfields = new List<string>();
                    //Get all values by reflection
                    Type t = GetType();
                    FieldInfo[] fields = GetType().GetFields();
                    foreach (FieldInfo field in fields)
                    {
                        if (field.IsStatic)
                        {
                            allstaticfields.Add((string)field.GetValue(this));
                        }
                    }
                    allstaticfields.Sort();
                }
                return allstaticfields;
            }
        }

        /// <summary>
        /// Check if a command is a valid command, the comparison is made case
        /// insensitive
        /// </summary>
        /// <param name="fieldname"></param>
        /// <returns></returns>
        public bool IsStaticField(string fieldvalue)
        {
            foreach (string field in AllStaticFields)
            { 
                if(field.ToLower().Equals(fieldvalue.ToLower()))
                    return true;
            }
            return false;
        }

        /// <summary>
        /// Correct the casing of a fieldvalue
        /// </summary>
        /// <param name="fieldvalue"></param>
        public void CorrectCasing(ref string fieldvalue)
        {
            foreach (string field in AllStaticFields)
            {
                if (field.ToLower().Equals(fieldvalue.ToLower()))
                {
                    fieldvalue = field;
                }
            }
        }
    }
}
